#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include <errno.h>
const char * s = "./";
const int commonid = 0x11223344;
const char * path = "fifo"; // 写
int main()
{
    if(access(path, F_OK) < 0)
    {
        mkfifo(path, 0666);
    }
    key_t key = ftok(s, commonid);
    printf("%d\n", key);
    if(key < 0)
    {
        perror("ftok");
        printf("%s\n", strerror(errno));
        return 0;
    }

    int shmid = shmget(key, 1024, IPC_CREAT | IPC_EXCL | 0666);
    printf("%d\n",shmid);
    if(shmid < 0)
    {
        perror("shmid");
        return 0;
    }
    char * p = (char*)shmat(shmid, NULL, 0);
    int fd = open(path, O_RDONLY);
    int code = 0, cnt = 0;
    while(1)
    {
        int n = read(fd, &code, sizeof(code));
        if(n > 0)
        {
            if(code)
            {
                printf("%s\n", p);
            }
        }
        else if(n == 0)
        {
            printf("写进程退出\n");
            break;
        }
        else
        {
            perror("read");
            break;
        }
        printf("%d\n", cnt++);
    }
    close(fd);
    shmdt(p); // 消除映射
    shmctl(shmid, IPC_RMID, NULL); // 减少计数
    return 0;
}